$(function() {
    var layer = layui.layer
    getUserInfo()


    $('.logOut').on('click', function() {
        // console.log(11);
        layer.confirm('确定退出登录?', { icon: 3, title: '提示' }, function(index) { // do something
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index);
        })

    })
})

function getUserInfo() {
    $.ajax({
        method: 'GET',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // },
        success: function(res) {
            // console.log(res);
            if (res.status !== 0) return layer.msg('获取用户信息失败!')
            renderAvatar(res.data)
        }
    })
}


function renderAvatar(user) {
    var name = user.nickname || user.username
        // console.log(name);
    $('.welcome').html('欢迎&nbsp;&nbsp;' + name);
    if (user.user_pic !== null) {
        //渲染头像
        $('.layui-nav-img').attr('src', user.user_pic).show();
        $('.text-avatar').hide();
    } else {
        //渲染文字头像
        $('.layui-nav-img').hide();
        var first = name[0].toUpperCase();
        $('.text-avatar').show();

        $('.text-avatar').html(first).show()

    }
}